package com.example.common.util;

import java.util.*;
import java.util.stream.Collectors;

public class CollectionUtils extends org.apache.commons.collections.CollectionUtils {
	public static final CollectionUtils INSTANCE = new CollectionUtils();

	/**
	 * 移除空字符串
	 * 
	 * @param collections
	 */
	public static void removeEmptyString(Collection<String> collections) {
		List<String> removeStringList = new ArrayList<String>();
		for (String string : collections) {
			if (StringUtils.isEmpty(string)) {
				removeStringList.add(string);
			}
		}
		collections.removeAll(removeStringList);
	}

	/**
	 * 取最小的对象
	 * 
	 * @param collections
	 */
	public static <T extends Comparable<? super T>> List<T> min(Collection<T> collections) {
		T min = null;
		for (T e : collections) {
			if (min == null) {
				min = e;
			}
			if (e.compareTo(min) < 0) {
				min = e;
			}
		}
		return equal(collections, min);
	}

	/**
	 * 取最大的对象
	 * 
	 * @param collections
	 */
	public static <T extends Comparable<? super T>> List<T> max(Collection<T> collections) {
		T max = null;
		for (T e : collections) {
			if (max == null) {
				max = e;
			}
			if (e.compareTo(max) > 0) {
				max = e;
			}
		}
		return equal(collections, max);
	}

	/**
	 * 取相等值
	 * 
	 * @param <T>
	 * @param collections
	 * @param object
	 * @return
	 */
	public static <T extends Comparable<? super T>> List<T> equal(Collection<T> collections, T object) {
		List<T> returnList = new ArrayList<T>();
		for (T e : collections) {
			if (e.compareTo(object) == 0) {
				returnList.add(e);
			}

		}
		return returnList;
	}

	/**
	 * Return <code>true</code> if the supplied Collection is <code>null</code>
	 * or empty. Otherwise, return <code>false</code>.
	 * 
	 * @param collection
	 *            the Collection to check
	 * @return whether the given Collection is empty
	 */
	public static boolean isEmpty(Collection collection) {
		return (collection == null || collection.isEmpty());
	}

	/**
	 * coll1是否全包含coll2的值
	 * 
	 * @param coll1
	 * @param coll2
	 * @return
	 */
	public static <T> boolean containsAll(final Collection<T> coll1, final Collection<T> coll2) {
		// size为0
		if (coll1.size() <= 0 || coll2.size() <= 0){
			return false;
		}


		for (T t : coll2) {
			// 只要有一个不包含，则返回false
			if (!coll1.contains(t)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 将list反转排序
	 * 
	 * @param list
	 */
	public static void reverse(List<?> list) {
		if (list == null) {
			return;
		}
		Collections.reverse(list);
	}

	/**
	 * coll2中的元素在coll1中出现的个数
	 * 
	 * @param coll1
	 * @param coll2
	 * @return
	 */
	public static <T> int countMatchEach(final Collection<T> coll1, final Collection<T> coll2) {
		// size为0
		if (coll1.size() <= 0 || coll2.size() <= 0){
			return 0;
		}


		int count = 0;
		for (T t : coll2) {
			// 包含，加1
			if (coll1.contains(t)) {
				count++;
			}
		}
		return count;
	}

	public static void main(String[] args){
	    List<String> list1 = Arrays.asList(new String[]{"1", "2", "3", "4"});
		List<String> list2 = Arrays.asList(new String[]{"1", "2", "4", "3"});
		System.out.println(CollectionUtils.containsAll(list1, list2));
		List<Integer> i = list1.stream().map(item -> Convert.toInteger(item)).collect(Collectors.toList());
		System.out.println(i);
	}
}
